package com.zksite.nator.startup;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.zksite.nator.server.http.ForwardServer;
import com.zksite.nator.server.http.HttpServer;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

@Component
public class ApplicationListener implements org.springframework.context.ApplicationListener<ApplicationEvent> {

	@Autowired
	private ForwardServer forwardServer;

	@Autowired
	private HttpServer httpConnector;

	private EventLoopGroup workGroup = new NioEventLoopGroup();// 用于网络事件的处理

	private EventLoopGroup bossGruop = new NioEventLoopGroup();// 用于服务器端接受客户端的连接

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) {
			httpConnector.setLoopGroup(workGroup, bossGruop);
			forwardServer.setLoopGroup(workGroup, bossGruop);
			forwardServer.start();// 启动跳转服务器
			httpConnector.start();// 启动http连接器
		}
	}
}
